package com.tgu.home_action.service.listener;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

import com.tgu.home_action.pojo.state.RedisHeader;
import com.tgu.home_action.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;


public class KeyExpiredListener extends KeyExpirationEventMessageListener {
    private RedisService redisService;
    private static final Logger LOGGER = LoggerFactory.getLogger(KeyExpiredListener.class);


    public KeyExpiredListener(RedisMessageListenerContainer listenerContainer, RedisService redisService) {
        super(listenerContainer);
        this.redisService = redisService;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        System.out.println(message);
        String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
        //过期的key
        String key = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println(key);
        String[] keySplit = key.split("__");
        if (keySplit.length != 2) {
            return;
        }
        if (keySplit[0].equals(RedisHeader.VOLUNTEER_ONLINE.getHeader())) {
            redisService.volunteerOffLine(Integer.valueOf(keySplit[1]));
        } else if (keySplit[0].equals(RedisHeader.VOLUNTEER_ONLINE_TWO_HOUR.getHeader())) {
            redisService.volunteerOffLineOutTwoHour(Integer.valueOf(keySplit[1]));
        } else if (keySplit[0].equals(RedisHeader.CASE_TIME.getHeader())) {
            redisService.caseOverTime(Integer.valueOf(keySplit[1]));
        } else if (keySplit[0].equals("admin_access_token")) {

        }
    }
}